package edu.tgc.web.controller.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.common.exception.ServiceException;
import org.abyss.common.web.controller.easyui.CommonCrudEasyUiController;
import org.abyss.common.web.controller.easyui.EasyuiAjaxFormResult;
import org.abyss.common.web.controller.easyui.EasyuiResultBuilder;
import org.abyss.data.specifications.Rule;
import org.abyss.dictionary.service.DictionaryEasyUiService;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.tgc.custom.EasyuiTree;
import edu.tgc.entity.hrm.IdCade;
import edu.tgc.entity.hrm.dictionary.HrmEducationType;
import edu.tgc.entity.hrm.dictionary.HrmEmployeeStatus;
import edu.tgc.entity.system.SysDept;
import edu.tgc.entity.system.SysDeptPosition;
import edu.tgc.entity.system.SysPosition;
import edu.tgc.entity.system.SysRole;
import edu.tgc.entity.system.SysUser;
import edu.tgc.security.SessionUserUtils;
import edu.tgc.service.hrm.IdCadeService;
import edu.tgc.service.system.SysDeptService;
import edu.tgc.service.system.SysUserService;
import edu.tgc.web.form.system.SysUserForm;
import edu.tgc.web.form.system.SysUserForm.passwordValidate;

@Controller
@RequestMapping(value = "sys/user")
public class SysUserController extends CommonCrudEasyUiController<SysUser, UUID, SysUserForm> {

    protected class Role extends SysRole {

        private static final long serialVersionUID = -8360273004358975850L;
        protected String source;

        public Role(SysRole role, String source) {
            BeanUtils.copyProperties(role, this);
            this.source = source;
        }

        public String getSource() {
            return this.source;
        }

        public void setSource(String source) {
            this.source = source;
        }
    }

    @Autowired
    protected SysDeptService sysDeptService;
    @Autowired
    protected DictionaryEasyUiService dictionaryService;
    @Autowired
    protected SysUserService sysUserService;
    @Autowired
    protected IdCadeService idCadeService;

    @Override
    protected void buildIgnoreProperties(Collection<String> ignoreProperties) {
        ignoreProperties.add("password");
        ignoreProperties.add("roles");
        ignoreProperties.add("positions");
        ignoreProperties.add("deptPositions");
    }

    @Override
    public Specification<SysUser> buildSearch(SysUserForm form) {
        HashSet<Rule> rules = new HashSet<>();
        String username = form.getUsername();
        if (StringUtils.hasText(username)) {
            rules.add(Rule.property("username").contain(username));
        }
        IdCade cade=form.getCade();
        if(cade!=null){
        	String name = cade.getName();
        	if(StringUtils.hasText(name)){
        		rules.add(Rule.property("cade.name").contain(name));
        	}
        	String idNo = cade.getIdNo();
        	if(StringUtils.hasText(idNo)){
        		rules.add(Rule.property("cade.idNo").contain(idNo));
        	}
        	String sex = cade.getSex();
        	if(StringUtils.hasText(sex)){
        		rules.add(Rule.property("cade.sex").equal(sex));
        	}
        	String nation = cade.getNation();
        	if(StringUtils.hasText(nation)){
        		rules.add(Rule.property("cade.nation").contain(nation));
        	}
        }
        SysDept dept = form.getDept();
        if(dept!=null){
        	UUID deptId = dept.getId();
        	if(deptId!=null){
        		rules.add(Rule.property("dept.id").equal(deptId));
        	}
        }
        HrmEmployeeStatus status = form.getStatus();
        if(status!=null){
        	UUID statusId=status.getId();
        	if(statusId!=null){
        		rules.add(Rule.property("status.id").equal(statusId));
        	}
        }
        Boolean enabled = form.getEnabled();
        if(enabled!=null){
        	rules.add(Rule.property("enabled").equal(enabled));
        }else{
        	rules.add(Rule.property("enabled").equal(true));
        }
        Date createTimeB = form.getCreateTimeB();
        if(createTimeB!=null){
        	rules.add(Rule.property("createTime").greaterThanOrEqualTo(createTimeB));
        }
        return Rule.and(rules).<SysUser> toSpecification();
    }

    @ResponseBody
    @RequestMapping(value = "/checkUserName")
    public Object checkUserName(SysUserForm form, BindingResult bindingResult) {
        SysUser user = this.sysUserService.getByUsername(form.getUsername());
        return user == null || user.getId().equals(form.getId());
    }

    @ResponseBody
    @RequestMapping(value = "position/selected")
    public Object getPositions(String joinIds) {
        Set<Map<String, Object>> result = new LinkedHashSet<>();
        if (joinIds != null) {
            for (String entityId : joinIds.split(",")) {
                SysUser entity = this.baseService.getById(UUID.fromString(entityId));
                if (entity != null) {
                    for (SysDeptPosition deptPosition : entity.getDeptPositions()) {
                        Map<String, Object> position = new HashMap<>();
                        position.put("id", deptPosition.getDept().getId() + "_" + deptPosition.getPosition().getId());
                        position.put("name", deptPosition.getPosition().getName());
                        position.put("deptName", deptPosition.getDept().getName());
                        position.put("enabled", deptPosition.getPosition().getEnabled());
                        result.add(position);
                    }
                }
            }
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "role/selected")
    public Object getRoles(String joinIds, Boolean source) {
        Set<Role> result = new LinkedHashSet<>();
        if (joinIds != null) {
            for (String entityId : joinIds.split(",")) {
                SysUser entity = this.baseService.getById(UUID.fromString(entityId));
                if (entity != null) {
                    for (SysRole sysRole : entity.getRoles()) {
                        result.add(new Role(sysRole, "个人"));
                    }
                    if (source != null && source) {
                        for (SysRole sysRole : entity.getDept().getRoles()) {
                            result.add(new Role(sysRole, "部门"));
                        }
                    }
                }
            }
        }
        return result;
    }

    @RequestMapping(value = "/combotree")
    @ResponseBody
    public Object getUserTree(String id) {
        if (StringUtils.hasText(id)) {
            return new ArrayList<EasyuiTree>();
        }
        List<EasyuiTree> nodes = this.sysUserService.findTreeNodes();
        return nodes;
    }

    @ResponseBody
    @RequestMapping(value = "/password")
    public Object password(@Validated(value = passwordValidate.class) SysUserForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return EasyuiResultBuilder.buildAjaxResultError(bindingResult.getFieldError().getDefaultMessage());
        } else {
            try {
                this.sysUserService.setPassword(form.getId(), null);
            } catch (ServiceException e) {
                return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
            }
        }
        return EasyuiResultBuilder.buildAjaxResult("重置密码成功");
    }

    @ResponseBody
    @RequestMapping(value = "position", method = RequestMethod.POST)
    public Object position(SysUserForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return EasyuiResultBuilder.buildAjaxResultError(bindingResult.getGlobalError().getDefaultMessage());
        } else {
            try {
                Set<SysDeptPosition> deptPositions = new HashSet<>();
                if (form.getPositionIds() != null) {
                    for (String positionIds : form.getPositionIds()) {
                        String[] split = positionIds.split("_");
                        String deptId = split[0];
                        String positionId = split[1];
                        SysDeptPosition deptPosition = new SysDeptPosition(new SysDept(UUID.fromString(deptId)), new SysPosition(UUID.fromString(positionId)));
                        deptPositions.add(deptPosition);
                    }
                }
                this.sysUserService.setPosition(form.getId(), deptPositions);
            } catch (ServiceException e) {
                return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
            }
        }
        return EasyuiResultBuilder.buildAjaxResult();
    }

    @ResponseBody
    @RequestMapping(value = "role", method = RequestMethod.POST)
    public Object role(SysUserForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return EasyuiResultBuilder.buildAjaxResultError(bindingResult.getGlobalError().getDefaultMessage());
        } else {
            try {
                this.sysUserService.setRoles(form.getId(), form.getRoleIds());
            } catch (ServiceException e) {
                return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
            }
        }
        return EasyuiResultBuilder.buildAjaxResult();
    }
    
    @ResponseBody
    @RequestMapping(value = "self/password")
    public Object passwordSelf(@Validated(value = passwordValidate.class) SysUserForm form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return EasyuiResultBuilder.buildAjaxResultError(bindingResult.getFieldError().getDefaultMessage());
        } else {
            try {
                this.sysUserService.setPassword(SessionUserUtils.getSessionUser().getId(), form.getLastPassword(), form.getPassword());
            } catch (ServiceException e) {
                return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
            }
        }
        return EasyuiResultBuilder.buildAjaxResult("修改密码成功");
    }

    @RequestMapping(value = "self/password", method = RequestMethod.GET)
    public void password(SysUserForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
    }


	
    @Override
	public EasyuiAjaxFormResult create(SysUserForm form, BindingResult bindingResult, Model model,
			RedirectAttributes redirectAttributes) {
		try{
			UUID id=form.getCade().getId();
			IdCade idCade=idCadeService.findById(id);
			idCade.setIsUsed(true);
			idCadeService.save(idCade);
			return super.create(form, bindingResult, model, redirectAttributes);
		}catch (Exception e) {
			return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
		}
	}

	@Override
	public EasyuiAjaxFormResult update(SysUserForm form, BindingResult bindingResult, Model model,
			RedirectAttributes redirectAttributes) {
		try {
			UUID id=form.getId();
			SysUser user=sysUserService.findById(id);
			UUID cadeId=(user.getCade()==null?null:user.getCade().getId());
			UUID formCaseId=(form.getCade()==null?null:form.getCade().getId());
			if(!(cadeId==null&&formCaseId==null||cadeId!=null&&cadeId.equals(formCaseId))){
				if(user.getCade()!=null){
					IdCade cade=idCadeService.getById(cadeId);
					cade.setIsUsed(false);
					idCadeService.save(cade);
				}
				if(formCaseId!=null){
					IdCade cade=idCadeService.getById(formCaseId);
					cade.setIsUsed(true);
					idCadeService.save(cade);
				}
			}
			return super.update(form, bindingResult, model, redirectAttributes);
			
		} catch (Exception e) {
			return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
		}
	}

	@Override
	public EasyuiAjaxFormResult delete(SysUserForm form, BindingResult bindingResult, Model model,
			RedirectAttributes redirectAttributes) {
		SysUser user=sysUserService.findById(form.getId());
		IdCade cade=user.getCade();
		if(cade!=null){
			cade.setIsUsed(false);
			idCadeService.save(cade);
		}
		return super.delete(form, bindingResult, model, redirectAttributes);
	}
	
	@RequestMapping(value="/exportNull",method=RequestMethod.GET)
	public String exportUser(List<UUID> ids,HttpServletRequest request,HttpServletResponse response){
		
		return "sys/user/exprotNull";
	}
	
	@RequestMapping(value="/exportSpan")
	public Object exportSpan(HttpServletRequest request , HttpServletResponse response) throws IOException{
		String[] excelHeader = { "姓名", "身份证号码","用户名","性别","民族","出生日期","家庭住址","开始时间","结束时间","发证机关","部门","人员种类","手机号码","qq号码","邮件","学历","专业"};
		HSSFWorkbook wb = new HSSFWorkbook();  
        HSSFSheet sheet = wb.createSheet("员工信息导入表");  
        HSSFRow row = sheet.createRow((int) 0);  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        
        for(int i=0;i<excelHeader.length;i++){
        	HSSFCell cell=row.createCell(i);
        	cell.setCellValue(excelHeader[i]);
        	cell.setCellStyle(style);  
            sheet.setColumnWidth(i, 16*256); 
        }
        HSSFPatriarch patr=sheet.createDrawingPatriarch();
        HSSFClientAnchor anchor = patr.createAnchor(0, 0, 0, 0, excelHeader.length, 1, excelHeader.length+3, 2);//创建批注位置
        HSSFComment comment = patr.createCellComment(anchor);//创建批注
        comment.setString(new HSSFRichTextString("值为\"男\"，\"女\""));//设置批注内容
        comment.setAuthor("罗建川");//设置批注作者
        comment.setVisible(false);//设置批注默认显示
        HSSFCell cell = row.getCell(3);
        cell.setCellComment(comment);//把批注赋值给单元格
        anchor=patr.createAnchor(0, 0, 0, 0, excelHeader.length, 3, excelHeader.length+3, 4);
        comment=patr.createCellComment(anchor);
        comment.setAuthor("罗建川");
        comment.setString(new HSSFRichTextString("身份证的起始有效时间"));
        cell=row.getCell(7);
        cell.setCellComment(comment);
        anchor=patr.createAnchor(0, 0, 0, 0, excelHeader.length, 5, excelHeader.length+3, 6);
        comment=patr.createCellComment(anchor);
        comment.setAuthor("罗建川");
        comment.setString(new HSSFRichTextString("身份证的结束有效时间"));
        cell=row.getCell(8);
        cell.setCellComment(comment);
        String str1=sysDeptService.getLeverl2DeptName();
        anchor=patr.createAnchor(0, 0, 0, 0, excelHeader.length, 7, excelHeader.length+3, 8);
        comment=patr.createCellComment(anchor);
        comment.setAuthor("罗建川");
        comment.setString(new HSSFRichTextString(str1));
        cell=row.getCell(10);
        cell.setCellComment(comment);
        Sort sort=new Sort("sort");
        @SuppressWarnings("unchecked")
		List<HrmEmployeeStatus> list = (List<HrmEmployeeStatus>) dictionaryService.getList(HrmEmployeeStatus.class,sort);
        String str="";
		for (HrmEmployeeStatus status : list) {
			str+=status.getName()+"\r\n";
		}
		if(str.length()>0){
			str=str.substring(0, str.length()-1);
		}
		str="可填值为：\r\n"+str;
        anchor=patr.createAnchor(0, 0, 0, 0, excelHeader.length, 9, excelHeader.length+3, 10);
        comment=patr.createCellComment(anchor);
        comment.setAuthor("罗建川");
        comment.setString(new HSSFRichTextString(str));
        cell=row.getCell(11);
        cell.setCellComment(comment);
        
        @SuppressWarnings({"unchecked" })
		List<HrmEducationType> list2 = (List<HrmEducationType>) dictionaryService.getList(HrmEducationType.class,sort);
        str="";
        for(HrmEducationType type : list2){
        	str+=type.getName()+"\r\n";
        }
		if(str.length()>0){
			str=str.substring(0, str.length()-1);
		}
		str="可填值为：\r\n"+str;
        anchor=patr.createAnchor(0, 0, 0, 0, excelHeader.length, 11, excelHeader.length+3, 12);
        comment=patr.createCellComment(anchor);
        comment.setAuthor("罗建川");
        comment.setString(new HSSFRichTextString(str));
        cell=row.getCell(15);
        cell.setCellComment(comment);
        //设定身份证号，电话和qq为字符类型
        HSSFCellStyle style2 = wb.createCellStyle();
        HSSFDataFormat format = wb.createDataFormat(); 
        style2.setDataFormat(format.getFormat("@"));
        sheet.setDefaultColumnStyle(12, style2);
        sheet.setDefaultColumnStyle(13, style2);
        sheet.setDefaultColumnStyle(1, style2);
        response.setContentType("application/vnd.ms-excel");  
        response.setHeader("Content-disposition", "attachment;filename=customer.xls");  
        OutputStream ouputStream = response.getOutputStream();  
        wb.write(ouputStream);  
        ouputStream.flush();  
        ouputStream.close();
        wb.close();
		return null;
	}
    
	@RequestMapping(value="/exprotManage",method=RequestMethod.GET)
	public String exprotManage(){
		return "sys/user/exprotManage";
	}
	
	@RequestMapping(value="/exportHrm")
	@ResponseBody
	public Object exportHrm(SysUserForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String[] excelHeader = { "姓名", "身份证号码","用户名", "性别","民族","出生日期","家庭住址","开始时间","结束时间","发证机关","部门","人员种类","手机号码","qq号码","邮件","学历","专业","是否激活"};
		HSSFWorkbook wb = new HSSFWorkbook();  
		HSSFSheet sheet = wb.createSheet("员工信息"); 
		
		HSSFRow row = sheet.createRow((int) 0);  
		HSSFCellStyle style = wb.createCellStyle();  
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		
		for(int i=0;i<excelHeader.length;i++){
			HSSFCell cell=row.createCell(i);
			cell.setCellValue(excelHeader[i]);
			cell.setCellStyle(style);  
			sheet.setColumnWidth(i, 16*256); 
		}
		Order order=new Order(Direction.ASC, "dept.sort");
		Order order2=new Order(Direction.ASC,"sort");
		List<Order> orders=new ArrayList<>();
		orders.add(order);
		orders.add(order2);
		Sort sort=new Sort(orders);
		List<SysUser> list=baseService.getList(buildSearch(form),sort);
		for (int j = 0; j < list.size(); j++) {
			SysUser sysUser = list.get(j);
			row=sheet.createRow(j+1);
			HSSFCell cell=row.createCell(0);
			cell.setCellValue(sysUser.getCade().getName());
			cell.setCellStyle(style);
			
			cell=row.createCell(1);
			cell.setCellValue(sysUser.getCade().getIdNo());
			cell.setCellStyle(style);
			
			cell=row.createCell(2);
			cell.setCellValue(sysUser.getUsername());
			cell.setCellStyle(style);

			cell=row.createCell(3);
			cell.setCellValue(sysUser.getCade().getSex());
			cell.setCellStyle(style);
			
			cell=row.createCell(4);
			cell.setCellValue(sysUser.getCade().getNation());
			cell.setCellStyle(style);
			
						
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			cell=row.createCell(5);
			cell.setCellValue(sdf.format(sysUser.getCade().getBorn()));
			cell.setCellStyle(style);
			
			cell=row.createCell(6);
			cell.setCellValue(sysUser.getCade().getAddress());
			cell.setCellStyle(style);
			
			cell=row.createCell(7);
			cell.setCellValue(sdf.format(sysUser.getCade().getUserLifeB()));
			cell.setCellStyle(style);
			
			cell=row.createCell(8);
			cell.setCellValue(sdf.format(sysUser.getCade().getUserLifeE()));
			cell.setCellStyle(style);
			
			cell=row.createCell(9);
			cell.setCellValue(sysUser.getCade()==null?"":sysUser.getCade().getPolice());
			cell.setCellStyle(style);
			
			cell=row.createCell(10);
			cell.setCellValue(sysUser.getDept()==null?"":sysUser.getDept().getName());
			cell.setCellStyle(style);
			
			cell=row.createCell(11);
			cell.setCellValue(sysUser.getStatus()==null?"":sysUser.getStatus().getName());
			cell.setCellStyle(style);
			
			cell=row.createCell(12);
			cell.setCellValue(sysUser.getTelphone()==null?"":sysUser.getTelphone());
			cell.setCellStyle(style);
			
			cell=row.createCell(13);
			cell.setCellValue(sysUser.getQq()==null?"":sysUser.getQq());
			cell.setCellStyle(style);
			
			cell=row.createCell(14);
			cell.setCellValue(sysUser.getEmail()==null?"":sysUser.getEmail());
			cell.setCellStyle(style);
			
			cell=row.createCell(15);
			cell.setCellValue(sysUser.getEducation()==null?"":sysUser.getEducation().getName());
			cell.setCellStyle(style);
			
			cell=row.createCell(16);
			cell.setCellValue(sysUser.getMajor()==null?"":sysUser.getMajor());
			cell.setCellStyle(style);
			
			cell=row.createCell(17);
			cell.setCellValue(sysUser.getEnabled()?"是":"否");
			cell.setCellStyle(style);
		}
		UUID uid=UUID.randomUUID();
        File file=new File(IdCade.getPath(),uid.toString()+".down");
        OutputStream outputStream=new FileOutputStream(file);
        wb.write(outputStream);
        //outputStream.flush();
        outputStream.close();
        wb.close();
        return file.getName();
		/*String filename="员工基本情况表.xls";
		filename=getStr(request,filename);
        response.setContentType("application/vnd.ms-excel");  
        response.setHeader("Content-disposition", "attachment;filename="+filename);  
        OutputStream ouputStream = response.getOutputStream();  
        wb.write(ouputStream);  
        ouputStream.flush();  
        ouputStream.close(); 
        wb.close();
		return null;*/

	}
	@RequestMapping(value="/exportHrm1")
	@ResponseBody
	public Object exportHrm1(String fileName,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		File file=new File(IdCade.getPath(),fileName);
		if (file.exists()) {
            response.reset();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String filename = sdf.format(new Date()) + "员工基本数据表.xls";
            filename = getStr(request, filename);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[4096];
            InputStream in = new FileInputStream(file);
            int size;
            while ((size = in.read(buf)) != -1) {
                out.write(buf, 0, size);
            }
            in.close();
            out.flush();
            out.close();
            file.delete();
        }
        return null;
	}
	
    public String getStr(HttpServletRequest request, String fileName) {
        String downloadFileName = null;
        String agent = request.getHeader("USER-AGENT");
        try {
            if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
                downloadFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
            } else {
                downloadFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return downloadFileName;
    }
    
    @RequestMapping(value="/importHrmManage")
    public String importHrmManage(){
    	return "sys/user/importHrmManage";
    }
    
    @RequestMapping(value="/importHrm",method=RequestMethod.GET)
    public String importHrm(){
    	return "sys/user/importHrm";
    }

    @RequestMapping(value="/importHrm",method=RequestMethod.POST)
    @ResponseBody
    public Object importHrm(MultipartFile excelFile) throws IOException {
    	HSSFWorkbook wb=new HSSFWorkbook(excelFile.getInputStream());
    	try {
    		String msg=sysUserService.importHrm(wb);
    		//wb.close();
    		if(StringUtils.hasText(msg)){
    			return EasyuiResultBuilder.buildAjaxResult(msg);
    		}
    		return EasyuiResultBuilder.buildAjaxResult("用户数据导入成功");
		} catch (Exception e) {
			return EasyuiResultBuilder.buildAjaxResultError("用户导入失败");
		}
    }
    
    
    
}
